package de.dhbw.asa.model;

import java.sql.ResultSet;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 * This class provides everything for a single item of an order.
 * An order can have several items. The attributes of an item are listet in this class
 * @author aar
 */

@ManagedBean
@RequestScoped
public class Item extends Database
{

	public int iItemNr;
	public int iOrderNr;
	public int iQuantity;
	public int iProductNr;
	
	//Not for the database:
	public String sProductTitle;
	public float fPrice;
	public int iPosition;
	
	/*
	 * this method loads a given item of an order
	 */
	public boolean load(int iItemNr)
	{
		boolean bReturn = false;
		String sItemNr = String.valueOf(iItemNr);
		StringBuffer sbQuery = new StringBuffer();
		sbQuery.append("Select * from Item where itemNr = '");
		sbQuery.append(sItemNr);
		sbQuery.append("';");
		try
		{
			if (this.createConnection())
			{
				ResultSet jResult = this.executeQuery(sbQuery.toString());
				if (jResult.next())
				{
					this.iItemNr = iItemNr;
					this.iOrderNr = Integer.parseInt(jResult.getString("itemNr"));
					this.iQuantity = Integer.parseInt(jResult.getString("quantity"));
					this.iProductNr = Integer.parseInt(jResult.getString("productNr"));
				}
				bReturn = true;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return bReturn;
	}
	
	/*
	 * this method saves changes of an item
	 */
	public String saveChanges()
	{
		String sReturn = "";
		try
		{
			this.createConnection();
			StringBuffer sbQuery = new StringBuffer();
			// Insert the Order into the Database
			sbQuery.append("update item set orderNr=");
			sbQuery.append(this.iOrderNr);
			sbQuery.append(", quantity =");
			sbQuery.append(this.iQuantity);
			sbQuery.append(", productNr =");
			sbQuery.append(this.iProductNr);
			sbQuery.append(" where itemNr =");
			sbQuery.append(this.iItemNr);
			sbQuery.append(";");
			this.execute(sbQuery.toString());
			this.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			sReturn = e.getMessage();
		}
		return sReturn;
	}
	
	/*
	 * this method saves a new item of an in the database
	 */
	public String saveNewItem(int iOrderNr, int iQuantity, int iProductNr)
	{
		String sReturn = "";
		this.iOrderNr = iOrderNr;
		this.iQuantity = iQuantity;
		this.iProductNr = iProductNr;
		try
		{
			this.createConnection();
			StringBuffer sbQuery = new StringBuffer();
			// Finding out the last ItemNr of our Items
			sbQuery.append("Select itemNr from Item;");
			ResultSet jResult = this.executeQuery(sbQuery.toString());
			int iLastItemNr = 0;
			while (jResult.next())
			{
				iLastItemNr = jResult.getInt("itemNr");
			}
			this.iItemNr = iLastItemNr+1;
			StringBuffer sbQuery2 = new StringBuffer();
			// Insert the item into the Database
			sbQuery2.append("insert into item (itemNr, OrderNr, Quantity, productNr) values (");
			sbQuery2.append(this.iItemNr);
			sbQuery2.append(",");
			sbQuery2.append(this.iOrderNr);
			sbQuery2.append(",");
			sbQuery2.append(this.iQuantity);
			sbQuery2.append(",");
			sbQuery2.append(this.iProductNr);
			sbQuery2.append(");");
			this.execute(sbQuery2.toString());
			this.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			sReturn = e.getMessage();
		}
		return sReturn;
	}

	public int getiItemNr() 
	{
		return iItemNr;
	}

	public void setiItemNr(int iItemNr) 
	{
		this.iItemNr = iItemNr;
	}

	public int getiOrderNr() 
	{
		return iOrderNr;
	}

	public void setiOrderNr(int iOrderNr) 
	{
		this.iOrderNr = iOrderNr;
	}

	public int getiQuantity() 
	{
		return iQuantity;
	}

	public void setiQuantity(int iQuantity) 
	{
		this.iQuantity = iQuantity;
	}

	public int getiProductNr() 
	{
		return iProductNr;
	}

	public void setiProductNr(int iProductNr) 
	{
		this.iProductNr = iProductNr;
	}

	public String getsProductTitle() 
	{
		return sProductTitle;
	}

	public void setsProductTitle(String sProductTitle) 
	{
		this.sProductTitle = sProductTitle;
	}

	public float getfPrice() 
	{
		return fPrice;
	}

	public void setfPrice(float fPrice) 
	{
		this.fPrice = fPrice;
	}

	public int getiPosition() {
		return iPosition;
	}

	public void setiPosition(int iPosition) {
		this.iPosition = iPosition;
	}
	
	
	
}
